//
//  MovieViewController.m
//  美食百科
//
//  Created by dllo on 16/2/2.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "MovieViewController.h"
#import "MJExtension.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"
#import "NewWorking.h"
#import "Food.h"
#import "MovieTableViewCell.h"
#import "FoodViewController.h"
#import "MoreMovieViewController.h"
#import "Reachability.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface MovieViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)MovieModel *movie;
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *movieArray;
@end

@implementation MovieViewController
- (void)dealloc
{
    [_movie release];
    [_tableView release];
    [_movieArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.movieArray = [NSMutableArray array];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"美食教程";
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        
        //如果无网络,弹窗提示
        if ( 0 == reach.currentReachabilityStatus){
            //弹窗提示
            UILabel *nonconnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
            nonconnectionLabel.textAlignment = NSTextAlignmentCenter;
            nonconnectionLabel.backgroundColor = [UIColor redColor];
            nonconnectionLabel.text = @"网络不给力,刷新试试~";
            nonconnectionLabel.textColor = [UIColor whiteColor];
            [self.view addSubview:nonconnectionLabel];
            
            //动画效果,通知栏缓慢出现
            [UIView animateWithDuration:0.6 animations:^{
                nonconnectionLabel.frame = CGRectMake(0, 0, kWidth, 50);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.6 delay:3 options:UIViewAnimationOptionAutoreverse animations:^{
                    nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                    [nonconnectionLabel release];
                    
                } completion:^(BOOL finished) {
                    
                }];
                
                //            [UIView animateWithDuration:1.9 animations:^{
                //                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                //            //[nonconnectionLabel removeFromSuperview];
                //                [nonconnectionLabel release];
                //            }];
                
            }];
            
        }
    });

    UIBarButtonItem *leftItemFirst = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-zuojiantou-3"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];

    self.navigationItem.leftBarButtonItem = leftItemFirst;
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SceneInfo",@"methodName",[NSNumber numberWithInt:20],@"size",[NSNumber numberWithInt:1], @"page",[NSNumber numberWithInt:51],@"scene_id",[NSNumber numberWithFloat:4.3],@"version", nil];
    
    [NewWorking PostDayaWithURL:@"http://api.izhangchu.com/" dic:dictionary sussess:^(id responseObject) {
        self.movie = [[MovieModel alloc] init];
        [self.movie setValuesForKeysWithDictionary:responseObject[@"data"]];
        
        NSArray *array =[responseObject[@"data"] objectForKey:@"dishes_list"];
        
        
        self.movieArray = [Food mj_objectArrayWithKeyValuesArray:array];
        
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
        self.tableView.backgroundColor = [UIColor clearColor];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
        self.tableView.bounces = NO;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.view addSubview:self.tableView];
    [_tableView release];
    [self.tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"movie"];
        
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"movieOne"];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -kHeight/5*2, kWidth, kHeight/5*2)];
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.movie.scene_background] placeholderImage:[UIImage imageNamed:@"u=1023456699,842778947&fm=21&gp=0"]];
    self.tableView.tableHeaderView = imageView;
    [imageView release];

        
        [self.tableView reloadData];
        
        
        
    } filed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];

    
    
        
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.movieArray.count+1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row != self.movieArray.count) {
        return (kHeight-kHeight/5*2)/3.5;
    }else
    {
        return 44;
    }
    
  }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row != self.movieArray.count) {
        
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movie"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.food = self.movieArray[indexPath.row];
    return cell;
        
    }else
    {
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieOne"];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.text = @"查看更多美食教程";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }
    

    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != self.movieArray.count) {
    //跳转介绍界面
        FoodViewController *foodVC = [[FoodViewController alloc]init];
        foodVC.food = self.movieArray[indexPath.row];

        [self.navigationController pushViewController:foodVC animated:YES];
        
        [foodVC release];
    }else
    {
        //跳转更多视频教程界面
        MoreMovieViewController *more = [[MoreMovieViewController alloc]init];
        more.array = self.movieArray;
        [self.navigationController pushViewController:more animated:YES];
        
        [more release];
}

}

-(void)backAction:(UIBarButtonItem *)item{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = YES;
}
- (BOOL)prefersStatusBarHidden{
    NSLog(@"ne");
    return NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
